﻿using ServiceCore.DbContext;
using ServiceCore.Entity.Const;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Model;
using System.Reflection;
using StaticVar = ServiceCore.Entity.Const.StaticVar;

namespace ServiceCore.Service.Common
{
    public class EnumService : SqlsugarServices<EnumInfo>
    {
        /// <summary>
        /// 枚举初始化
        /// </summary>
        /// <param name="Vpn"></param>
        /// <returns></returns>
        public BaseParam<EnumInfo, bool> Enum_Init(BaseParam<EnumInfo, bool> arg)
        {
            var path = StaticVar.HttpPath + StaticVar.PathSplitLine;
            var asm = Assembly.LoadFrom(path + @"ServiceCore.Entity.dll");

            List<Type> lsttype = asm.GetTypes().ToList<Type>();

            var lstInit = new List<EnumInfo>();

            int classcount = 0;
            foreach (Type onetype in lsttype)
            {
                var lstattribute = onetype.GetCustomAttributes(typeof(ConstAttribute), false).ToList();
                if (lstattribute.Count() == 0)
                    continue;
                //获得类别的attribute
                var classattribute = (ConstAttribute)lstattribute[0];
                var classConst = new EnumInfo();
                classConst.EnumId = onetype.Name;
                classConst.EnumParentId = "";
                classConst.EnumValue = classattribute.Description;
                classConst.SortBy = classcount;
                classConst.Remark = classattribute.Remark;
                if (lstInit.Where(x => x.EnumId == onetype.Name).Count() > 0)
                {
                    arg.ErrorMsg = "类:" + onetype.Name + "重复";
                    return arg;
                }
                lstInit.Add(classConst);
                classcount++;
                //获得字段的attribute
                int fieldcount = 0;
                var lstfield = onetype.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public).ToList();
                foreach (FieldInfo fi in lstfield)
                {
                    lstattribute = fi.GetCustomAttributes(typeof(ConstAttribute), true).ToList();
                    if (lstattribute.Count() == 0)
                        continue;
                    var Fieldattribute = (ConstAttribute)lstattribute[0];

                    var fieldConst = new EnumInfo();
                    fieldConst.EnumId = fi.GetValue(null).ToString();
                    fieldConst.EnumParentId = classConst.EnumId;
                    fieldConst.EnumValue = Fieldattribute.Description;
                    fieldConst.SortBy = fieldcount;
                    if (string.IsNullOrEmpty(Fieldattribute.Remark) == false)
                        fieldConst.Remark = Fieldattribute.Remark;
                    else
                        fieldConst.Remark = classattribute.Remark;                  
                    lstInit.Add(fieldConst);
                    fieldcount++;
                }
            }

            this.Context.Insertable<EnumInfo>(lstInit).ExecuteCommand();
            arg.Data = true;
            
            return arg;
        }

        /// <summary>
        /// 列表 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<EnumInfo, List<EnumInfo>> Enum_List(BaseParam<EnumInfo, List<EnumInfo>> arg)
        {
            var lstEnumParentId = new List<string>();
            if (arg.Param != null && string.IsNullOrEmpty(arg.Param.EnumParentId) == false)
            {
                lstEnumParentId.AddRange(arg.Param.EnumParentId.Split(','));
                lstEnumParentId = lstEnumParentId.Where(x => string.IsNullOrWhiteSpace(x) == false).ToList();
            }

            if (lstEnumParentId.Any())
                arg.Data = GlobalVar.lstEnum.Where(x => lstEnumParentId.Contains(x.EnumParentId)).OrderBy(x => x.SortBy).ToList();
            else
                arg.Data = GlobalVar.lstEnum.OrderBy(x => x.SortBy).ToList();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 类型列表 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<string, List<EnumInfo>> Enum_TypeList(BaseParam<string, List<EnumInfo>> arg)
        {
            var ParentIds = new List<string>();
            if(arg != null && string.IsNullOrEmpty(arg.Param) == false)
                ParentIds = arg.Param.Split(",").ToList();
            if (ParentIds.Any())
            {
                arg.Data = GlobalVar.lstEnum.Where(x => ParentIds.Contains(x.EnumId)).ToList();
                return arg;
            }

            arg.Data = GlobalVar.lstEnum.Where(x => string.IsNullOrEmpty(x.EnumParentId)).ToList();
            return arg;
        }

        /// <summary>
        /// 缓存枚举变量
        /// </summary>
        public void Enum_Cache()
        {
            if (GlobalVar.lstEnum == null)
            {
                GlobalVar.lstEnum = this.Context.Queryable<EnumInfo>().ToList();
            }
        }
    }
}
